package webService;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.representation.Form;

/**
 *
 * @author Rasztemberg
 */
public class ServiceTest {
    
    
    public static String testUserRestService(String url){
         
         ClientConfig config = new DefaultClientConfig();
         Client client = Client.create(config);
         WebResource service = client.resource(getBaseURI(url));
         // Get JSON for application
         try{
            return service.path("services").path("users").accept(MediaType.APPLICATION_JSON).get(String.class);
         }  catch (UniformInterfaceException ex) { 
            ClientResponse cr = ex.getResponse(); 
            return null;
        }
    }
    private static URI getBaseURI(String url) {
        return UriBuilder.fromUri(url).build();
  }
    
    static {
	    //for localhost testing only
	    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
	    new javax.net.ssl.HostnameVerifier(){
 
	        public boolean verify(String hostname,
	                javax.net.ssl.SSLSession sslSession) {
	            if (hostname.equals("localhost")) {
	                return true;
	            }
	            return false;
	        }
	    });
	}
}
